<?php
	//Initial date: 2009-06-02
	//Last change: 2009-06-02 by Kamil Jaskiewicz
	
	include("classes/DBTable.class.php");
	include("classes/enum.class.php"); 

	$module_name = "Enum values";
	$action = $_REQUEST['action'];
	$enums = new enum();

	if ($_REQUEST['submit'])
	{
		$id		= $_REQUEST['id'];
		$name = $_REQUEST['name'];

		if ($action == 'edit')
		{
			$enums->DB->query("UPDATE dictionary SET name='$name' WHERE dict_id='$id'");
		} 
		else if ($action == 'add')
		{
			$dg = $_REQUEST['dict_group'];
			$q = "INSERT INTO dictionary VALUES ('', '$dg', '$name')";
			$enums->DB->query($q);			
		}
		header("location:enum.php");
		exit();
	}

	if ($action == 'delete')
	{
		$id = $_REQUEST['id'];
		if($id)
		{
			try
			{
			$enums->delete($id);
			header("location:enum.php");
			}
			catch (Exception $e)
			{
				print $e->getMessage(); 
			}
			exit();
		}
	}


	include("header.php");
	

	//action not defined, display all projects
	if (!$action)
	{
		$enums->selectAllGroups();
		$groups = array();

		while($enums->getNext())
		{
			$groups[] = $enums->dict_group;
		}

		foreach($groups as $group)
		{
			print("<table id='grid'>");
			print("<tr><td colspan='4'><b>$group</b></td></tr>\n");
			$enums->selectValuesForGroup($group);
			while($enums->getNext())
			{
				print("<tr>");
				print("<td>$enums->name</td>");
				print("<td><a href='enum.php?action=edit&id=$enums->dict_id'>Edit</a></td>");
				print("<td><a href='enum.php?action=delete&id=$enums->dict_id' onclick='return confirmation(\"Are you sure?\");' >Delete</a></td>");
				print("<td style='width:80%'>&nbsp;</td>");
				print("</tr>\n\n");
			}
			print("<tr><td colspan='4' style='text-align:right'><a href='enum.php?action=add&dg=$group'>Add new value</a></td></tr>");
			print("</table>");
		}
	}

	if ($action == 'edit' || $action == 'add')
	{
		$id=$_REQUEST['id'];
		if ($id)
			$enums->findID($id);
		print("<form action='enum.php' method='post'>");
		if ($action == 'edit')
		{
			print("<input type='hidden' name='action' value='edit' />");
			print("<input type='hidden' name='id' value='$id' />");
		}
		else if ($action == 'add')
		{
			$dict_group = $_REQUEST['dg'];
			print("<input type='hidden' name='action' value='add' />");
			print("<input type='hidden' name='dict_group' value='$dict_group' />");
		}
		print("<input type='hidden' name='submit' value='true' />");
		print("<table id='grid'>");
		print("<tr><td><b>Value</b></td><td><input type='text' value='$enums->name' name='name'/></td></tr>\n");
		print("<tr><td colspan='2'><input type='submit' value='Submit' />&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='Cancel' onclick='history.back();' /></td></tr>\n");
		print("</table>");
		print("</form>");
	}
	
	include("footer.php");
?>
